// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marathons: [],
    // name: String,  //id
    // start_time: String,
    // stop_time: String,
    // address: String,
    // organizer: String,
    // participants: String,
    // detail: String,
    // votes_for_teacher: String,
    // weight_for_teacher: String,
    // votes_for_student: String,
    // dimensions_for_vote: String,
    // status: String //0：未开始 1: 报名  2：投票 3：已结束
    images: [],
    auth: 0,
  },

  generateImages: function (num = 12) {
    const image_list = ["/images/shuta.png", "/images/hema.png", "/images/huanxiong.png", "/images/haiyaoyu.png", "/images/houzi.png", "/images/baitu.png", "/images/yanjingshe.png", "/images/she.png", "/images/hudie.png", "/images/pangxie.png", "/images/daishu.png", "/images/luotuo.png", "/images/tuoniao.png", "/images/hashiqi.png"]

    var images_temp = []
    for (var i = 0; i < num; i++) {
      var picture = Math.floor(Math.random() * 14)
      images_temp.push(image_list[picture])
    }
    return images_temp
    
  },

  //事件处理函数
  new_vote: function (e) {
    wx.navigateTo({
      url: '../hackson_new/hackson_new',
    })
  },

  jumpToDetail: function (e) {
    var that = this
    console.log(e.currentTarget)
    var index = e.currentTarget.dataset.idx
    wx.navigateTo({
      url: '../hackson_my_vote/hackson_my_vote?marathon=' + JSON.stringify(that.data.marathons[index])
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      auth: getApp().globalData.kind,
    })

    // that.generateImages()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //获取已有马拉松列表
    wx.request({
      url: getApp().globalData.my_server + "/hackson/marathons",
      data: {},
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          var images_temp = that.generateImages(res.data.marathons.length)
          console.log(images_temp)
          that.setData({
            marathons: res.data.marathons,
            images: images_temp
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀～',
          content: '获取已有马拉松列表失败',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function (res) {

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})